package com.dehui.traders.widgets;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

import com.dehui.traders.ui.home.HomeFragment;
import com.dehui.traders.ui.market.rankdetail.RankDetailActivity;
import com.dehui.traders.ui.optioanl.MyOptiaonalStockFragemnt;
import com.dehui.traders.ui.stockdetail.StocksDetailActivity;
import com.dehui.traders.ui.strategy.StrateDetailFragment;
import com.dehui.traders.ui.market.broader.TheBroaderMarketFragment;
import com.dehui.traders.ui.market.index.TheIndexMarketFragment;

/*
 * 自定义的 滚动控件
 * 重载了 onScrollChanged（滚动条变化）,监听每次的变化通知给 观察(此变化的)观察者
 * 可使用 AddOnScrollChangedListener 来订阅本控件的 滚动条变化
 * */
public class MyHScrollView extends HorizontalScrollView {

	private float mDownX;
	private float mDownY;
	Fragment fragment;
	Activity activity;

	public void setFragemnt(Fragment fragment){
		this.fragment = fragment;
	}

	public void setActivity(Activity mActivity) {
		this.activity = mActivity;
	}

	public MyHScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyHScrollView(Context context) {
		super(context);
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		//进行触摸赋值
		if (fragment == null && activity == null)
			return super.onTouchEvent(ev);

		if (fragment != null){

			if ( fragment instanceof TheBroaderMarketFragment){
				((TheBroaderMarketFragment)fragment).mTouchView = this;
			}
			if (fragment instanceof TheIndexMarketFragment){
				((TheIndexMarketFragment)fragment).mTouchView = this;
			}
			if (fragment instanceof HomeFragment){
				((HomeFragment)fragment).mTouchView = this;
			}
			if (fragment instanceof StrateDetailFragment){
				((StrateDetailFragment)fragment).mTouchView = this;
			}
			if (fragment instanceof MyOptiaonalStockFragemnt){
				((MyOptiaonalStockFragemnt)fragment).mTouchView = this;
			}

		}

		if (activity != null){

			if (activity instanceof RankDetailActivity){
				((RankDetailActivity)activity).mTouchView = this;
			}

			if (activity instanceof StocksDetailActivity){
				((StocksDetailActivity)activity).mTouchView = this;
			}

		}


		return super.onTouchEvent(ev);
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		//当当前的CHSCrollView被触摸时，滑动其它
		if (fragment != null){
			if (fragment instanceof TheBroaderMarketFragment && ((TheBroaderMarketFragment)fragment).mTouchView == this)
				((TheBroaderMarketFragment)fragment).onScrollChanged(l, t, oldl, oldt);
			else if (fragment instanceof TheIndexMarketFragment && ((TheIndexMarketFragment)fragment).mTouchView == this)
				((TheIndexMarketFragment)fragment).onScrollChanged(l, t, oldl, oldt);
			else if (fragment instanceof HomeFragment && ((HomeFragment)fragment).mTouchView == this)
				((HomeFragment)fragment).onScrollChanged(l, t, oldl, oldt);
			else if (fragment instanceof StrateDetailFragment && ((StrateDetailFragment)fragment).mTouchView == this)
				((StrateDetailFragment)fragment).onScrollChanged(l, t, oldl, oldt);
			else if (fragment instanceof MyOptiaonalStockFragemnt && ((MyOptiaonalStockFragemnt)fragment).mTouchView == this)
				((MyOptiaonalStockFragemnt)fragment).onScrollChanged(l, t, oldl, oldt);
			else
				super.onScrollChanged(l, t, oldl, oldt);

		}

		if (activity != null){

			if (activity instanceof RankDetailActivity && ((RankDetailActivity)activity).mTouchView == this)
				((RankDetailActivity)activity).onScrollChanged(l, t, oldl, oldt);

			if (activity instanceof StocksDetailActivity && ((StocksDetailActivity)activity).mTouchView == this)
				((StocksDetailActivity)activity).onScrollChanged(l, t, oldl, oldt);

		}

		super.onScrollChanged(l, t, oldl, oldt);

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		switch (ev.getAction()){
			case MotionEvent.ACTION_DOWN:
				mDownX = ev.getX();
				mDownY = ev.getY();
				break;
			case MotionEvent.ACTION_MOVE:
				if (Math.abs(ev.getX() - mDownX) > Math.abs(ev.getY() - mDownY)){
					getParent().requestDisallowInterceptTouchEvent(true);
				}
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				break;
		}
		return super.dispatchTouchEvent(ev);
	}

}
